

if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "XM1014"			
	SWEP.Author				= "Counter-Strike"
	SWEP.Slot				= 2
	SWEP.SlotPos			= 0
	SWEP.IconLetter			= "B"
	
	killicon.AddFont( "weapon_xm1014", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )
	
end

SWEP.HoldType			= "ar2"
SWEP.Base				= "weapon_cs_base2_ds"

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.ViewModel 			= "models/weapons/v_shot_xm1014.mdl"
SWEP.WorldModel 		= "models/weapons/w_shot_xm1014.mdl"

SWEP.Weight				= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false
SWEP.ReloadDelay		= 0.55

SWEP.Primary.Sound			= Sound( "Weapon_XM1014.Single" )
SWEP.Primary.Recoil			= 4
SWEP.Primary.Damage			= 5
SWEP.Primary.NumShots		= 8
SWEP.Primary.Cone			= 0.053
SWEP.Primary.ClipSize		= 7
SWEP.Primary.Delay			= 0.25
SWEP.Primary.DefaultClip	= 39
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "buckshot"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"


function SWEP:Reload()
	
	//if ( CLIENT ) then return end
	// Already reloading
	if self.Weapon:GetNetworkedBool( "reloading", false ) then return end
	// Start reloading if we can
	if self.Weapon:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount( self.Primary.Ammo ) > 0 then
		
		self.Weapon:SetNetworkedBool( "reloading", true )
		self.Weapon:SetNetworkedFloat( "reloadtimer", CurTime() + self.ReloadDelay )
		self.Weapon:SendWeaponAnim( ACT_VM_RELOAD )
		self.Owner:DoReloadEvent()
			
	end

end

/*---------------------------------------------------------
   Think does nothing
---------------------------------------------------------*/
function SWEP:Think()
	if !self.Weapon:GetNetworkedBool( "reloading", true ) and self.Weapon:Clip1() < 1 and !self.Owner:KeyDown( IN_ATTACK ) then
		timer.Simple( self.Primary.Delay,function() self:Reload() end)
	end

	if self.Weapon:GetNetworkedBool( "reloading", false ) then
	
		if self.Weapon:GetNetworkedFloat( "reloadtimer", 0 ) < CurTime() then
			
			// Finsished reload -
			if self.Weapon:Clip1() >= self.Primary.ClipSize or self.Owner:GetAmmoCount( self.Primary.Ammo ) <= 0   then
				self.Weapon:SetNetworkedBool( "reloading", false )
				return
			end
			
			// Next cycle
			self.Weapon:SetNetworkedFloat( "reloadtimer", CurTime() + self.ReloadDelay )
			self.Weapon:SendWeaponAnim( ACT_VM_RELOAD )
			self.Owner:DoReloadEvent()
			
			// Add ammo
			self.Owner:RemoveAmmo( 1, self.Primary.Ammo, false )
			self.Weapon:SetClip1(  self.Weapon:Clip1() + 1 )
			
			// Finish filling, final pump
			if self.Weapon:Clip1() >= self.Primary.ClipSize or self.Owner:GetAmmoCount( self.Primary.Ammo ) <= 0 then
				self.Weapon:SendWeaponAnim( ACT_SHOTGUN_RELOAD_FINISH )
				self.Owner:DoReloadEvent()
			end
			
		end
	
	end

end

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	
	if self.Owner:KeyDown( IN_ATTACK ) and self.Weapon:Clip1() < 1 and self.Weapon:GetNetworkedBool( "reloading", false ) then
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay / 2 )
		return false
	end
	self.Weapon:SetNetworkedBool( "reloading", false )

	if !self:CanPrimaryAttack() or self.Owner:WaterLevel() > 2 or self.Weapon:Clip1() < 1 then return end

	// Shoot the bullet
	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	local Crouch = ( self.Owner:Crouching() and self.Owner:OnGround() ) and 2 or 1

	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone / math.Clamp( ( CurTime() - LastShootTime ) * 1.3 , 0.001, 1 ) / Crouch )
		
	// Remove 1 bullet from our clip
	
	if self.Owner:IsNPC() then return end

	// Punch the player's view
	self.Owner:ViewPunch( Angle( math.Rand( -0.5, -0.1 ) * self.Primary.Recoil, math.Rand( -0.5, 0.5 ) *self.Primary.Recoil, 0 ) )
	
	// In singleplayer this function doesn't get called on the client, so we use a networked float
	// to send the last shoot time. In multiplayer this is predicted clientside so we don't need to 
	// send the float.
	if ( SinglePlayer() and SERVER ) or CLIENT then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
end


function SWEP:CSShootBullet( dmg, recoil, numbul, cone )

	numbul = numbul or 1
	cone = cone or 0.01
	cone = ( self.Owner:GetVelocity():Length() > 150 ) and cone + ( self.Owner:GetVelocity():Length() / 10000 ) or cone

	local bullet = {}
	bullet.Num 		= numbul
	bullet.Src 		= self.Owner:GetShootPos()			// Source
	bullet.Dir 		= self.Owner:GetAimVector()			// Dir of bullet
	bullet.Spread 	= Vector( cone, cone, 0 )			// Aim Cone
	bullet.Tracer	= 2									// Show a tracer on every x bullets 
	bullet.Force	= 5									// Amount of force to give to phys objects
	bullet.Damage	= dmg

	self.Owner:FireBullets( bullet )
	self:TakePrimaryAmmo( 1 )
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
	self.Owner:MuzzleFlash()
	self.Weapon:EmitSound( self.Primary.Sound )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )								// Crappy muzzle light				// 3rd Person Animation
	if self.Owner:IsNPC() then return end
	
	if ( (SinglePlayer() && SERVER) || ( !SinglePlayer() && CLIENT && IsFirstTimePredicted() ) ) then
	
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles( eyeang )
	end
end